package com.tmc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthDept;
import com.tmc.service.baseinfo.DeptServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping(value="/dept")
public class DeptController extends BaseController{
	@Autowired
	private DeptServiceImpl deptService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView findist(EthDept cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<EthDept> page = deptService.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		return new ModelAndView("views/dept/dept-list", map);
	}
	
	@RequestMapping(value = "/editPage")
	public ModelAndView editPage(Long deptId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		EthDept dept = null;
		if (deptId != null)
			dept = deptService.getById(context, deptId);
		else
			dept = new EthDept();
		map.put("dept", dept);
		return new ModelAndView("views/dept/dept-edit", map);
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.GET)
	@ResponseBody
	public String save(EthDept dept, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = deptService.save(context, dept);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/delete", method=RequestMethod.GET)
	@ResponseBody
	public String delete(Long deptId, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = deptService.delete(context, deptId);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/search", method=RequestMethod.POST)
	@ResponseBody
	public String search(String query){
		List<EthDept> list=null;
		list = deptService.searchDept(query);
		JSONArray array = new JSONArray();
		for(EthDept dept : list){
			//"Key":"clientId","Name":"userName" 注意，键和值都是字符串
			JSONObject obj = new JSONObject();
			obj.put("Key", dept.getDeptId() + "");
			obj.put("Name", dept.getDeptName() + "");
			array.add(obj);
		}
 		return array.toJSONString();	
	}
}
